<template>
	<view>
		<view class="statbar"></view>
		<view style="margin: 20px;">
			<u-search placeholder="请输入剧名" :clearabled="true" v-model="keyword" :showAction="true" @search="search" :actionText="actionText" @custom="custom"
				@clear="keyword=''" @blur="blur" @focus="focus"></u-search>
		</view>
		<view style="margin: 20px; font-size: 14px;color: #6d6d6d;">
			<view style="display: flex;flex-direction: row;"><view>搜索历史</view>
			<view style="position: relative;left: calc(100% - 70px);"><u-icon name='trash' size="large" @click="clearHistory"></u-icon></view>
			</view>
			<view style="float: left;" v-if="refresh">
				<view v-for="item,index in history" style="float: left;margin-top: 10px;font-size: 13px;margin-right: 10px;margin-bottom: 10px;">
					<view style="border-radius: 10px; background-color: #C8C7CC;padding: 2px 10px 2px 10px;" @click="searchH(item)">{{item}}</view>
				</view>
			</view>
		</view>
		<view style="clear: both;margin:20px; font-size: 16px;color: #383838;">
			<view style="padding-bottom:15px;">实时热榜</view>
			<view v-for="item,index in hotList" style="font-size: 14px;margin-bottom: 15px;">
				<view @click="toVodDetail(item)">{{index+1}}.  {{item.vodName}}
				<span v-if="index<3" style="margin-left: 10px;font-size: 10px;color: #FFFFFF;background-color:#FF0000;padding: 2px;border-radius: 2px; height: 80%;">热</span>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getHotList	} from '../../config/api.js';
	export default {
		data() {
			return {
				actionText: '取消',
				keyword:'',
				history: [],
				refresh: true,
				hotList: [],
			}
		},
		onLoad() {
			var  searchHistoty = uni.getStorageSync('searchHistoty');
			if(searchHistoty){
				this.history = searchHistoty
			}
			this.findHotList()
		},
		methods: {
			toVodDetail(item) {
				if(uni.getStorageSync('playerSet') == 1){
					uni.$u.route({
						url: 'pages/dplayer/dplayer',
						params: {
							vodId: item.vodId
						}
					})
				}else{
					uni.$u.route({
						url: 'pages/detail/detail',
						params: {
							vodId: item.vodId
						}
					})
				}


			},
			findHotList(){
				getHotList().then((res) => {
					this.hotList = res
				})
			},
			search(value) {
				this.history.push(value);
				uni.setStorageSync('searchHistoty',this.history);
				uni.$u.route({
					url: 'pages/list/list-page',
					params: {
						keyword: value,
						navText: "搜索 "+value+" 结果"
					}
				})
			},
			searchH(value) {
				uni.$u.route({
					url: 'pages/list/list-page',
					params: {
						keyword: value,
						navText: "搜索 "+value+" 结果"
					}
				})
			},
			custom(value) {
				console.log(this.actionText)
				if (this.actionText == '搜索') {
					this.history.push(value);
					uni.setStorageSync('searchHistoty',this.history);
					uni.$u.route({
						url: 'pages/list/list-page',
						params: {
							keyword: value,
							navText: "搜索 "+value+" 结果"
						}
					})
				} else if (this.actionText == '取消') {
					uni.$u.route({
						type: 'navigateBack',
						url: 'pages/home/home'
					})

				}
			},
			blur(){
				setTimeout(()=>{
					this.actionText="取消"
				},500)

			},
			focus(){
				this.actionText="搜索"
			},
			clearHistory(){

				this.refresh = false;
				setTimeout(()=>{
					this.history=[];
					uni.setStorageSync('searchHistoty',this.history);
					this.refresh = true
				},100)

			}
		}
	}
</script>

<style lang="scss" scoped>
  .statbar {
		height: var(--status-bar-height);
		background: #FFFFFF;
	}
</style>
